import 'activity_data.dart';
import 'pagination_data.dart';

/// 查询活动列表响应模型
class ActivityListResponse {
  final String status;
  final String? message;
  final List<ActivityData> data;
  final PaginationData? pagination;

  ActivityListResponse({
    required this.status,
    this.message,
    required this.data,
    this.pagination,
  });

  /// 从JSON创建实例
  factory ActivityListResponse.fromJson(Map<String, dynamic> json) {
    return ActivityListResponse(
      status: json['status'] ?? 'error',
      message: json['message'],
      data: json['data'] != null && json['data'] is List
          ? (json['data'] as List)
              .map((item) => ActivityData.fromJson(item))
              .toList()
          : [],
      pagination: json['pagination'] != null
          ? PaginationData.fromJson(json['pagination'])
          : null,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      if (message != null) 'message': message,
      'data': data.map((item) => item.toJson()).toList(),
      if (pagination != null) 'pagination': pagination!.toJson(),
    };
  }

  /// 判断是否成功
  bool get isSuccess => status == 'success';

  /// 判断是否有错误
  bool get hasError => status == 'error';

  /// 获取错误信息
  String get errorMessage => message ?? '未知错误';

  /// 判断是否为空数据
  bool get isEmpty => data.isEmpty;

  /// 判断是否有数据
  bool get hasData => data.isNotEmpty;

  /// 获取活动总数
  int get totalCount => pagination?.total ?? data.length;

  /// 判断是否还有更多数据
  bool get hasMore => pagination?.hasNextPage ?? false;

  /// 获取当前页
  int get currentPage => pagination?.page ?? 1;

  /// 获取活动数量
  int get activityCount => data.length;

  /// 按距离排序活动（从近到远）
  List<ActivityData> get sortedByDistance {
    final List<ActivityData> sortedData = List.from(data);
    sortedData.sort((a, b) {
      if (a.distance == null && b.distance == null) return 0;
      if (a.distance == null) return 1;
      if (b.distance == null) return -1;
      return a.distance!.compareTo(b.distance!);
    });
    return sortedData;
  }

  /// 按发布时间排序活动（从新到旧）
  List<ActivityData> get sortedByTime {
    final List<ActivityData> sortedData = List.from(data);
    sortedData.sort((a, b) {
      if (a.minutesAgo == null && b.minutesAgo == null) return 0;
      if (a.minutesAgo == null) return 1;
      if (b.minutesAgo == null) return -1;
      return a.minutesAgo!.compareTo(b.minutesAgo!);
    });
    return sortedData;
  }

  /// 筛选正在进行的活动
  List<ActivityData> get activeActivities {
    return data.where((activity) => activity.isActive).toList();
  }

  /// 筛选未满员的活动
  List<ActivityData> get availableActivities {
    return data.where((activity) => !activity.isFull).toList();
  }

  /// 根据活动类型筛选
  List<ActivityData> filterByType(String type) {
    return data.where((activity) => 
        activity.activityType.contains(type)).toList();
  }

  /// 根据标签筛选
  List<ActivityData> filterByTag(String tag) {
    return data.where((activity) => 
        activity.activityTags.contains(tag)).toList();
  }

  /// 根据距离筛选（小于等于指定距离）
  List<ActivityData> filterByDistance(double maxDistance) {
    return data.where((activity) => 
        activity.distance != null && activity.distance! <= maxDistance).toList();
  }

  @override
  String toString() {
    return 'ActivityListResponse{status: $status, dataCount: ${data.length}, hasMore: $hasMore}';
  }
}
